<template>
  <view class="container">
    <!--input列表 -->
    <view class="input-box">
      <view class="section">
        <text>手机号码</text>
        <input
          placeholder="11位手机号码"
          type="number"
          maxlength="11"
          :value="mobile"
          @input="onMobileInput"
        >
      </view>
      <view class="section" style="align-items:start;">
        <text>问题</text>
        <textarea placeholder="问题" :value="question" @input="onQuestionInput"></textarea>
      </view>
    </view>
    <!-- end input列表 -->
    <!-- 功能按钮 -->
    <view class="btn-box">
      <view
        class="keep btn"
        @tap="onSaveQuestion"
      >
        <text>保存问题</text>
      </view>

      <view
        v-if="questionId!=0"
        class="clear btn"
        @tap="onDeleteQuestion"
      >
        <text>删除问题</text>
      </view>
    </view>
    <!-- end 功能按钮 -->
  </view>
</template>

<script setup>
const questionId = ref('')
const mobile = ref('')
const question = ref('')
onLoad((options) => {

  options.questionId = options.questionId || 0;
  if (options.questionId) {
    questionId.value = options.questionId;
    uni.showLoading()

    http.request({
      url: '/p/question/questionInfo/' + options.questionId,
      method: 'GET'
    })
      .then(({ data }) => {
        mobile.value = data.mobile
        question.value = data.question
        uni.hideLoading()
      })
  } else {
    fillMockData();
  }
})

const provArray = ref([])
const valArr = ref([0, 0, 0])

/**
 * 保存地址
 */
const onSaveQuestion = () => {
  const mobileParam = mobile.value
  const questionParam = question.value

  if (!mobileParam) {
    uni.showToast({
      title: '请输入手机号码',
      icon: 'none'
    })
    return
  }

  if (!mobileParam.trim()) {
    uni.showToast({
      title: '请输入正确的手机号码',
      icon: 'none'
    })
    return
  }

  if (!questionParam.trim()) {
    receiver.value = ''
    uni.showToast({
      title: '请输入问题',
      icon: 'none'
    })
    return
  }

  uni.showLoading()
  let url = '/p/question/addQuestion'
  let method = 'POST'

  if (questionId.value != 0) {
    url = '/p/question/updateQuestion'
    method = 'PUT'
  } // 添加或修改地址

  http.request({
    url,
    method,
    data: {
      mobile: mobile.value,
      question: question.value,
      questionId: questionId.value
    }
  })
    .then(() => {
      uni.hideLoading()
      uni.navigateBack({
        delta: 1
      })
    })
}
const onReceiverInput = (e) => {
  receiver.value = e.detail.value
}
const onMobileInput = (e) => {
  mobile.value = e.detail.value
}
const onQuestionInput = (e) => {
  question.value = e.detail.value
}

/**
 * 删除配送地址
 */
const onDeleteQuestion = () => {
  uni.showModal({
    title: '',
    content: '确定要删除此问题吗？',
    confirmColor: '#eb2444',

    success (res) {
      if (res.confirm) {
        const questionIdParam = questionId.value
        uni.showLoading()
        http.request({
          url: '/p/question/deleteQuestion/' + questionIdParam,
          method: 'DELETE'
        })
          .then(() => {
            uni.hideLoading()
            uni.navigateBack({
              delta: 1
            })
          })
      }
    }
  })
}

const fillMockData = () => {
  // 直接填充当前表单的数据
  mobile.value = "13800138000";  // 填充手机号
  question.value = "这是一个测试问题，用于快速填充数据";  // 填充问题描述
};

</script>

<style scoped lang="scss">
@use './questionEdit.scss';
</style>
fid